﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Security;

namespace haber
{
    /// <summary>
    /// Summary description for ek
    /// </summary>
    public class ek : IHttpHandler
    {

        private void UpdateCookie(string cookie_name, string cookie_value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
            if (null == cookie)
            {
                cookie = new HttpCookie(cookie_name);
            }
            cookie.Value = cookie_value;
            HttpContext.Current.Request.Cookies.Set(cookie);
        }


        public void ProcessRequest(HttpContext context)
        {


            context.Response.ContentType = "text/plain";

            context.Response.Expires = -1;

            try
            {

                HttpPostedFile postedFile = context.Request.Files[0];



                string savepath = "";

               
                //  string dosyaAdi = context.Request["filename"];
                string tip = context.Request["tip"];
                string id = context.Request["id"];
                string name = context.Request["name"];
                //if (String.IsNullOrEmpty(dosyaAdi))
                //{
                //    context.Response.StatusCode = 3117;
                //    context.Response.Write("Hata: " + "Upload Hatası. Lütfen programı kapatıp açıp tekrar yüklemeyi deneyin.");
                //    return;
                //}
                if (id == "G")
                {
                    savepath = context.Server.MapPath("~/Ekler/");
                }
                else
                {
                    savepath = context.Server.MapPath("~/gidenEkler/");

                }
                

              

                if (!Directory.Exists(savepath))
                    Directory.CreateDirectory(savepath);



                //                String uzanti = Path.GetExtension(postedFile.FileName);

                //if (File.Exists(savepath + @"\" + postedFile.FileName))
                //{
                //    throw new Exception("Aynı isimli dosya mevcut, lütfen yeni isimle kaydediniz.");
                //}

                postedFile.SaveAs(savepath + @"\" + name);

               
                
                context.Response.Write(name);

                context.Response.StatusCode = 200;
              

            }

            catch (Exception ex)
            {

                context.Response.Write("Error: " + ex.Message);

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}